package com.yitiao.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yitiao.domain.SysMenu;
import com.yitiao.domain.SysRoleMenu;
import com.yitiao.domain.SysRoleUser;
import com.yitiao.domain.Users;
import com.yitiao.mapper.SysMenuMapper;
import com.yitiao.mapper.SysRoleMenuMapper;
import com.yitiao.mapper.SysRoleUserMapper;
import com.yitiao.service.UsersService;
import com.yitiao.mapper.UsersMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author libiao
 * @description 针对表【users】的数据库操作Service实现
 * @createDate 2022-09-30 10:16:35
 */
@Service
@RequiredArgsConstructor
public class UsersServiceImpl implements UsersService {
    private final UsersMapper usersMapper;
    private final SysRoleUserMapper roleUserMapper;
    private final SysRoleMenuMapper roleMenuMapper;
    private final SysMenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        Users users = usersMapper.selectOne(Wrappers.lambdaQuery(Users.class).eq(Users::getUsername, name));
        Assert.isTrue(ObjectUtil.isNotNull(users), StrUtil.format("user [{}] is not exist"), name);
//        String encode = new BCryptPasswordEncoder().encode(users.getPassword());
        // 获取用户的权限
        List<GrantedAuthority> auths = new ArrayList<>();
        List<SysRoleUser> sysRoleUsers = roleUserMapper.selectList(Wrappers.lambdaQuery(SysRoleUser.class)
                .eq(SysRoleUser::getUid, users.getId())
        );
        sysRoleUsers.forEach(sysRoleUser -> {
            List<SysRoleMenu> sysRoleMenus = roleMenuMapper.selectList(Wrappers.lambdaQuery(SysRoleMenu.class)
                    .eq(SysRoleMenu::getRid, sysRoleUser.getRid())
            );
            sysRoleMenus.forEach(sysRoleMenu -> {
                List<SysMenu> sysMenus = menuMapper.selectList(Wrappers.lambdaQuery(SysMenu.class)
                        .eq(SysMenu::getId, sysRoleMenu.getMid())
                );
                sysMenus.forEach(sysMenu -> {
                    auths.add(new SimpleGrantedAuthority(sysMenu.getPermission()));
                });

            });
        });
        return new User(users.getUsername(), users.getPassword(), auths);
    }
}




